package com.coin.exchanges.view.user

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.widget.Toast
import com.coin.exchanges.R
import com.coin.exchanges.net.RetrofitFactory
import com.coin.exchanges.presenter.TestPresenter
import com.coin.exchanges.utils.LCSharedPreferencesHelper
import com.coin.exchanges.utils.SpUtils
import com.coin.exchanges.view.user.bean.BindCardBean
import com.coin.libbase.view.activity.JToolbarActivity
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_used_charge.*

class UsedChargeActivity : JToolbarActivity<TestPresenter>(){
    override fun getLayout(): Int {
        return R.layout.activity_used_charge  }

    override fun initIntent(intent: Intent?) {
    }

    override fun initView() {
        setToolbarBackground(R.color.white)
        title = "充值"
    }

    override fun initData() {
        mBindChargeTv.setOnClickListener {
            if(checkBindCard(mPersonChargeNumEt.text.toString())){
                val sharedPreferencesHelper = LCSharedPreferencesHelper.instance(this@UsedChargeActivity, "register")

                val userId = sharedPreferencesHelper.getValue("UserId")

                PostNetChargeCheckAll(userId,mPersonChargeNumEt.text.toString())
            }
        }
        mPersonChargeIv.setOnClickListener {
            mPersonChargeNumEt.setText("")
        }
    }
    private fun PostNetChargeCheckAll(user_id: String,money: String){
        val map = HashMap<String, String>()
        map["user_id"] = user_id
        map["money"] = money

        RetrofitFactory
                .getOkExApiService()
                .charge(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : Observer<BindCardBean> {
                    override fun onComplete() {
                    }

                    override fun onSubscribe(d: Disposable) {
                    }

                    override fun onNext(t: BindCardBean) {
                        mPersonTipTv.text = t.returnval
                        Toast.makeText(this@UsedChargeActivity, t.returnval, Toast.LENGTH_LONG).show()
                        //finish()
                    }

                    override fun onError(e: Throwable) {
                        Log.e("jignan",e.message)
                    }

                })
    }
    fun checkBindCard(count: String): Boolean {
        if (TextUtils.isEmpty(count)) {
            Toast.makeText(this, "请输入充值金额", Toast.LENGTH_LONG).show()
            return false
        }
        return true}
}
